<template>
    <view
        class="category-bar"
        :class="[
            {
                'is-active': top > 80
            }
        ]"
    >
        <text
            class="category-bar__item is-index"
            :class="[
                {
                    'is-active': !id
                }
            ]"
            @click="toHome"
            >首页</text
        >

        <scroll-view
            scroll-x
            class="category-bar__scroller"
            scroll-with-animation
            :scroll-left="scrollLeft"
        >
            <text
                class="category-bar__item"
                v-for="(item, index) in conf.floorList"
                :key="index"
                :class="[
                    {
                        'is-active': item.floorId == id
                    },
                    'category-click-' + item.id
                ]"
                @click="select(item.floorId)"
                >{{ item.mainName }}</text
            >
        </scroll-view>
    </view>
</template>

<script>
import { mapGetters } from 'vuex';
export default {
    props: {
        value: Number,
        top: {
            type: Number,
            default: 0
        }
    },

    data() {
        return {
            id: null
        };
    },

    watch: {
        value: {
            immediate: true,
            handler(val) {
                this.id = val;
            }
        }
    },

    computed: {
        ...mapGetters(['conf']),
        scrollLeft() {
            const index = (this.conf.floorList || []).findIndex(e => e.floorId == this.id);
            return (index - 1) * 60;
        }
    },

    created() {
        this.refresh();
    },

    methods: {
        refresh() {
            this.$store.dispatch('getConf');
        },

        select(floorId) {
            this.id = floorId;

            this.$emit('input', floorId);
            this.$emit('change', floorId);
        },

        toHome() {
            this.$_router.push({
                mode: 'reLaunch',
                path: '/pages/index/home'
            });
        }
    }
};
</script>

<style lang="scss" scoped>
.category-bar {
    display: flex;
    align-items: center;
    height: 80rpx;
    line-height: 80rpx;
    width: 100%;
    z-index: 99;
    position: absolute;
    top: 0;
    overflow: hidden;

    &__scroller {
        box-sizing: border-box;
        white-space: nowrap;
        flex: 1;
        height: 96rpx;
        width: calc(100% - 120rpx);
    }

    &__item {
        display: inline-block;
        padding: 0 24rpx 10rpx 24rpx;
        font-size: 28rpx;
        position: relative;
        color: #fff;
        top: 4rpx;

        &.is-index {
            width: 80rpx;
            white-space: nowrap;
            text-align: center;
            top: 0;
        }

        &.is-active {
            font-size: 32rpx;

            &::after {
                content: '';
                display: block;
                height: 8rpx;
                width: 40rpx;
                background-color: currentColor;
                border-radius: 20rpx;
                position: absolute;
                left: calc(50% - 20rpx);
                bottom: 10rpx;
            }
        }
    }

    &.is-active {
        background-color: #fff;

        .category-bar__item {
            color: #000;

            &.is-active {
                color: $uni-color-primary;

                &::after {
                    background-color: $uni-color-primary;
                }
            }
        }
    }
}
</style>
